<template>
	<van-picker title="标题" show-toolbar default-index="value" :columns="columns" @confirm="onConfirm"
		@cancel="$emit('close')" @change="onChange" />
</template>

<script>
	import {
		updateUserProfile
	} from '@/api/user.js'

	export default {
		props: {
			value: {
				type: Number,
				required: true
			}
		},

		data() {
			return {
				columns: ['男', '女'],
				localGender: 0
			};
		},
		methods: {
			async onConfirm() {
				this.$toast.loading({
					message: '更新中...',
					forbidClick: true,
					duration: 0
				})

				try {
					const localGender = this.localGender
					const {
						data: res
					} = await updateUserProfile({
						gender: localGender
					})
					this.$emit('input', localGender)
					this.$emit('close')
					this.$toast.success("更新昵称成功")
				} catch (e) {
					this.$toast.fail("更新昵称失败")
				}
			},
			onChange(picker, value, index) {
				this.localGender = index
			},
		},
	}
</script>

<style scoped>

</style>
